using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data.Common;

namespace Tilver.DBConnectionUI.Editors
{
    [ToolboxItem(false)]
    internal partial class OracleEditor : UserControl, IUDLEditor
	{
        System.Data.OracleClient.OracleConnectionStringBuilder _builder =
            new System.Data.OracleClient.OracleConnectionStringBuilder();

        public OracleEditor()
		{
			InitializeComponent();

            this.txDataSource.Validated += delegate { _builder.DataSource = this.txDataSource.Text; };
            this.ckUseIntegratedSecurity.CheckedChanged += delegate { this.setSecurityMode(); };
            this.txUserID.Validated += delegate { _builder.UserID = this.txUserID.Text; };
            this.txPassword.Validated += delegate { _builder.Password = this.txPassword.Text; };
            this.ckPersistSecurity.CheckedChanged += delegate { _builder.PersistSecurityInfo = this.ckPersistSecurity.Checked; };
            this.ckUseIntegratedSecurity.Checked = true;
        }

        #region IConnectionEditor Members

        public string ConnectionString
        {
            get
            {
                return _builder.ConnectionString;
            }
            set
            {
                try
                {
                    _builder.ConnectionString = value;
                }
                catch (Exception) { }
                RefreshData();
            }
        }

        public void RefreshData()
        {
            this.txDataSource.Text = _builder.DataSource;
            this.txUserID.Text = _builder.UserID;
            this.txPassword.Text = _builder.Password;
            this.ckPersistSecurity.Checked = _builder.PersistSecurityInfo;
            this.ckUseIntegratedSecurity.Checked = _builder.IntegratedSecurity;
        }

        public object GetAdvancedPropertiesObject()
        {
            return _builder;
        }

        #endregion

        #region helper methods
        void setSecurityMode()
        {
            bool integrated = this.ckUseIntegratedSecurity.Checked;
            if (integrated)
            {
                _builder.IntegratedSecurity = true;
                ResetProperty("User ID");
                ResetProperty("Password");
                ResetProperty("Persist Security Info");
            }
            else
                _builder.IntegratedSecurity = false;

            this.txUserID.Text = _builder.UserID;
            this.txPassword.Text = _builder.Password;
            this.ckPersistSecurity.Checked = _builder.PersistSecurityInfo;

            this.txUserID.Enabled = !integrated;
            this.txPassword.Enabled = !integrated;
            this.ckPersistSecurity.Enabled = !integrated;
        }

        void SetValue(string key, object value)
        {
            if (_builder.ContainsKey(key))
                _builder[key] = value;
        }

        object GetValue(string key)
        {
            if (_builder.ContainsKey(key))
                return _builder[key];
            return null;
        }

        void ResetProperty(string key)
        {
            if (_builder.ContainsKey(key))
                _builder[key] = null;
            _builder.Remove(key);
        }

        #endregion
    }
}
